﻿using Manon.Admin.Web.Application.Dtos.Company;
using Manon.Admin.Web.Domain.Entities;
using Manon.Admin.Web.Domain.Repositories;
using Manon.Core;
using Manon.Core.Commons;
using Manon.Core.Extensions;
using Manon.Repository;
using Newtonsoft.Json;

namespace Manon.Admin.Web.Application.Impl
{
    public class CompanyService : ICompanyService
    {
        private readonly ILogger<CompanyService> _logger;
        private readonly IUnitOfWork _unitOfWork;
        private readonly ICompanyRepository _companyRepository;
        private readonly IApplicationUser _applicationUser;
        public CompanyService(ILogger<CompanyService> logger,
            IUnitOfWork unitOfWork,
            ICompanyRepository companyRepository,
            IApplicationUser applicationUser)
        {
            _logger = logger;
            _unitOfWork = unitOfWork;
            _companyRepository = companyRepository;
            _applicationUser = applicationUser;
        }

        /// <summary>
        /// 获取公司信息
        /// </summary>
        /// <param name="Input"></param>
        /// <returns></returns>
        public async Task<GetCompanyInfoOutput> GetCompanyInfo()
        {

            try
            {
                var result = await _companyRepository.GetFirstOrDefaultAsync();

                if (result == null) return null;

                return result.ConvertTo<Company, GetCompanyInfoOutput>();
            }
            catch (Exception ex)
            {
                _logger.LogError("获取公司信息异常", ex);
                throw;
            }
        }

        /// <summary>
        /// 保存公司信息
        /// </summary>
        /// <param name="Input"></param>
        /// <returns></returns>
        public async Task<Result> SaveCompanyInfo(SaveCompanyInfoInput Input)
        {
            try
            {
                var result = await _companyRepository.GetFirstOrDefaultAsync(x => x.Id == Input.Id);
                if (result == null)
                {
                    result = new Company();
                    result.Address = Input.Address;
                    result.BriefIntroduction = Input.BriefIntroduction;
                    result.CompanyName = Input.CompanyName;
                    result.Contact = Input.Contact;
                    result.CreateTime = DateTime.Now;
                    result.UpdateTime = DateTime.Now;
                    result.CreateBy = _applicationUser.UserId;
                    result.UpdateBy = _applicationUser.UserId;
                    await _companyRepository.InsertAsync(result);
                }
                else
                {
                    result.Address = Input.Address;
                    result.BriefIntroduction = Input.BriefIntroduction;
                    result.CompanyName = Input.CompanyName;
                    result.Contact = Input.Contact;
                    result.UpdateTime = DateTime.Now;
                    result.UpdateBy = _applicationUser.UserId;
                   await _companyRepository.UpdateAsync(result);
                }
                await _unitOfWork.SaveChangesAsync();
                return Result.Successed("保存成功");
            }
            catch (Exception ex)
            {
                _logger.LogError($"保存公司信息异常，入参input={JsonConvert.SerializeObject(Input)}", ex);
                throw;
            }
        }
    }
}
